#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
const int N = 2e3 + 10;
int arr[N][N];
int sum[N][N];
int n;
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int a, b, w;
		scanf("%d%d%d", &a, &b, &w);
		a++;
		b++;
		arr[a][b] += w;
	}
	for (int i = 1; i <= N - 1; i++) {
		for (int j = 1; j <= N - 1; j++) {
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + arr[i][j];
		}
	}
	int q;
	cin >> q;
	while (q--) {
		int x1, y1, x2, y2;
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		x1++, y1++, x2++, y2++;
		printf("%d\n", sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1]);
	}
	return 0;
}